﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WFL.IoCUnity;

namespace WFL.RBAC
{
    public class UserBLL
    {
        private static Dictionary<int, IList<Permission>> _userPermissionsCache = new Dictionary<int, IList<Permission>>(32);
        private static object _locker = new object();


        private IUser _user;

        public RoleBLL RoleBLL
        {
            get;
            private set;
        }

        public ResourceBLL ResourceBLL
        {
            get;
            private set;
        }

        public OperationBLL OperationBLL
        {
            get;
            private set;
        }

        public UserBLL()
        {
            _user = UnityUtility.Resolve<IUser>();
            RoleBLL = new RoleBLL();
            ResourceBLL = new ResourceBLL();
            OperationBLL = new OperationBLL();
        }


        public void BindRole(int userId, int roleId)
        {
            _user.BindRole(userId, roleId);
        }

        public void UnbindRole(int userId, int roleId)
        {
            _user.UnbindRole(userId, roleId);
        }

        public void UnbindRole(int userId)
        {
            _user.UnbindRole(userId);
        }

        public IList<Role> GetRoleList(int userId)
        {
            return _user.GetRoleList(userId);
        }

        public IList<T> GetLimitResourceList<T>(int userId, int resourceId)
        {
            throw new NotImplementedException();
        }

        public bool HasPermission(int userId, int resourceId, int operation, int limitResourceItemId)
        {
            throw new NotImplementedException();
        }

        public bool HasPermission(int userId, string resourceName, string operationName, int limitResourceName, int limitResourceItemId)
        {
            throw new NotImplementedException();
        }

        public bool HasPermission(int userId, Permission permission)
        {
            IList<Permission> userPermissions = null;
            if (_userPermissionsCache.ContainsKey(userId))
            {
                userPermissions = _userPermissionsCache[userId];
            }
            else
            {
                lock (_locker)
                {
                    if (!_userPermissionsCache.ContainsKey(userId))
                    {
                        IList<Role> roleList = _user.GetRoleList(userId);
                        if (roleList != null)
                        {
                            List<Permission> resultPermissions = new List<Permission>(8);
                            foreach (Role roleItem in roleList)
                            {
                                //获取每个角色的权限
                                IList<Permission> rolePermissions = RoleBLL.GetPermissionList(roleItem.Id);
                                if (rolePermissions != null)
                                {
                                    if (resultPermissions.Count == 0)
                                    {
                                        resultPermissions.AddRange(rolePermissions);
                                    }
                                    else
                                    {
                                        foreach (Permission rolePermission in rolePermissions)
                                        {
                                            Permission resultPermission = null;
                                            foreach (Permission tempResultPermission in resultPermissions)
                                            {
                                                if (tempResultPermission.ResourseId == rolePermission.ResourseId)
                                                {
                                                    resultPermission = tempResultPermission;
                                                    break;
                                                }
                                            }
                                            if (resultPermission == null)
                                            {
                                                resultPermissions.Add(rolePermission);
                                            }
                                            else
                                            {
                                                //合并相同资源的权限
                                                resultPermission.Union(rolePermission);
                                            }
                                        }
                                    }
                                }
                            }
                            if (resultPermissions.Count > 0)
                            {
                                _userPermissionsCache.Add(userId, resultPermissions);
                                userPermissions = resultPermissions;
                            }
                        }
                    }
                }
            }
            if (userPermissions != null)
            {
                foreach (Permission item in userPermissions)
                {
                    if (item.ResourseId == permission.ResourseId)
                    {
                        return item.Allowed(permission.Operations);
                    }
                }
            }
            return false;
        }
    }
}
